const mongoose = require('mongoose');
const News = require('../models/news.model');

//新增
exports.create=function(req,res,next){
    const news = new News(req.body);
    news.save().then((data)=>{
        res.json(data);
    })
}

exports.update=function(req,res,next){
    const id = req.params.id;
    News.findByIdAndUpdate(id,{$set:req.body},{new:false}).then(data=>{
        res.json(data);
    })
}


exports.remove=function(req,res,next){
    const id = req.params.id;
    News.findByIdAndDelete(id,(err,data)=>{
        res.json(data);
    })
}

exports.list=function(req,res,next){
    var page = req.body.page?req.body.page:1;
    var rows = req.body.rows?req.body.rows:1;

    var queryCondition = {};  //查询条件
    if(req.body.articleTitle&&req.body.articleTitle.trim().length>0){
        articleTitle   = req.body.articleTitle;
        queryCondition = {
            "articleTitle": new RegExp(articleTitle,'i')
        }
    }
    News.paginate(queryCondition,{page: + page,limit: + rows},function(err,result){
        result.rows = result.docs;
        delete result.docs;
        res.json(result);
    })
}

exports.getdata=function(req,res,next){
    var id = req.params.id;
    News.findById(id).then(data=>{
        res.json(data);
    })
}
exports.removes=function(req,res,next){
    var ids = req.body.ids.split(',');
    if(ids.length>0){
        News.remove({_id:{$in:ids}}).then(data=>{
            res.json({'message':'delete ok',status:200})
        })
    }

}
exports.catenews_0=function(req,res,next){  //生活新闻
    var articleKey = req.params.articleKey;
    // console.log(req.params.articleKey);
    News.find({"articleKey":"5be15727268e7c792cf7701f"}).then(data=>{
        res.json(data);
    })
}

exports.catenews_1=function(req,res,next){  //体育新闻
    var articleKey = req.params.articleKey;
    // console.log(req.params.articleKey);
    News.find({"articleKey":"5be15720268e7c792cf7701e"}).then(data=>{
        res.json(data);
    })
}

exports.catenews_2=function(req,res,next){  //娱乐新闻
    var articleKey = req.params.articleKey;
    // console.log(req.params.articleKey);
    News.find({"articleKey":"5be15711268e7c792cf7701d"}).then(data=>{
        res.json(data);
    })
}

